﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SharpLite.Domain.DataInterfaces;
using DAEProde.Domain;
using DAEProde.Tasks;

namespace DAEProde.Web.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class TournamentManagerController : Controller
    {
        private IRepository<Tournament> _tournamentRepository;
        private TournamentCudTasks _tournamentMgmtTasks;

        public TournamentManagerController(IRepository<Tournament> tournamentRepository, TournamentCudTasks tournamentMgmtTasks)
        {
            _tournamentRepository = tournamentRepository;
            _tournamentMgmtTasks = tournamentMgmtTasks;
        }

        //
        // GET: /TournamentManager/

        public ActionResult Index()
        {
            var x = _tournamentRepository.GetAll();

            return View(x.OrderBy(c => c.Name));
        }

        public ActionResult Create()
        {
            return View("Edit", _tournamentMgmtTasks.CreateEditViewModel());
        }

        public ActionResult Details(int id)
        {
            return View(_tournamentRepository.Get(id));
        }

        public ActionResult Edit(int id)
        {
            return View(_tournamentMgmtTasks.CreateEditViewModel(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Tournament tournament)
        {
            if (ModelState.IsValid)
            {
                ActionConfirmation<Tournament> confirmation = _tournamentMgmtTasks.SaveOrUpdate(tournament);

                if (confirmation.WasSuccessful)
                {
                    TempData["message"] = confirmation.Message;
                    return RedirectToAction("Index");
                }

                ViewData["message"] = confirmation.Message;
            }

            return View(_tournamentMgmtTasks.CreateEditViewModel(tournament));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            ActionConfirmation<Tournament> confirmation = _tournamentMgmtTasks.Delete(id);
            TempData["message"] = confirmation.Message;
            return RedirectToAction("Index");
        }

        public ActionResult ViewTeams(int id)
        {
            return View(_tournamentMgmtTasks.CreateEditViewModel(id));
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult ViewTeams(Tournament tournament)
        {
            if (ModelState.IsValid)
            {
                if (tournament.Teams.Count > 20)
                {
                    TempData["message"] = "El torneo no puede tener mas de 20 equipos";
                }
                else
                {
                    ActionConfirmation<Tournament> confirmation = _tournamentMgmtTasks.SaveTeams(tournament);

                    if (confirmation.WasSuccessful)
                    {
                        TempData["message"] = confirmation.Message;
                        return RedirectToAction("Index");
                    }

                    ViewData["message"] = confirmation.Message;
                }
            }

            return View(_tournamentMgmtTasks.CreateEditViewModel(tournament));
        }
    }
}
